<?php
namespace app\warehouse\controller;

use think\Db; 
class Item extends Base{

	public function details(){
        //echo "index";
        return view('details');
    }
	public function edit($pn){
        //echo "index";
        if($pn){
            $map['no'] = $pn;
            $info = Db::table('warehouse_item')
            ->where($map)
            ->find();
        }else{
            $info['name'] = "";
            $info['cid'] = 1;
            $info['cno'] = "A";
            $info['boxno'] = "01";
            $info['bagno'] = "01";
            $info['num'] = 0;
            $info['price'] = 0;
            $info['model'] = "";
            $info['note'] = "";

        }
        for($i=0;$i<100;$i++){
            $numarr[] = sprintf("%02d",$i);
        }
        foreach(range('A','Z') as $v){
            $strarr[] =  $v;
        }
        //var_dump($strarr);
        $categoryarr = $this->api_category_list();
        $this->assign('categoryarr',$categoryarr);
        $this->assign('numarr',$numarr);//00-99
        $this->assign('strarr',$strarr);//A-Z
        $this->assign('info',$info);//A-Z
        $this->assign('pn',$pn);//A-Z
        return view('edit');
    }

    public function save(){
        $pn = input("post.pn");
        $data['name'] = input("post.name");
        $data['cid'] = input("post.cid");
        $data['cno'] = input("post.cno");
        $data['boxno'] = input("post.boxno");
        $data['bagno'] = input("post.bagno");
        $data['num'] = input("post.num");
        $data['price'] = input("post.price");
        $data['model'] = input("post.model");
        $data['note'] = input("post.note");
        if($pn){
            Db::table('warehouse_item')->where('no', $pn)->update($data);//更新
        }else{
            $data['no'] = $this->api_make_pn();
            Db::table('warehouse_item')->insert($data);//新增
        }
        //返回成功
        $ret['status'] = 1;
        echo json_encode($ret);
    }
}